package com.redteam.models;

import com.redteam.data.beans.Person;

/**
 * Validates the users username and password.
 * @author Anthony
 */
public class AuthorizationModel {

    String userName;
    String password;
    DAOAccessor dao;
    Person person;

    /**
     * Constructor that takes a user name and password
     * @param userName The user name to check
     * @param password The password to check
     */
    public AuthorizationModel(String userName, String password) {
        this.userName = userName;
        this.password = password;
        person = null;
        dao = new DAOAccessor();
        if (!userName.equals("")) {
            person = dao.getPersonByUsername(userName);
        }

    }

    /**
     * Checks to see if the user exists.
     * @return Returns True is the user exists and False otherwise.
     */
    public boolean userExists() {
        boolean exists = false;
        if (person != null) {
            exists = true;
        }
        return exists;
    }

    /**
     * Returns the person who logged in
     * @return The person or null if the user name is invalid
     */
    public Person getUserType() {

        return person;

    }

    /**
     * Checks if the password typed matches the one in the database
     * @return Returns True if the passwords match and False otherwise.
     */
    public boolean checkPassword() {
        boolean valid = false;
        if (password.equals(person.getPassword())) {
            valid = true;
        }
        return valid;
    }
}
